/*--------------参数下拉-------------------*/
/*数字引脚*/
enum SZ_YINJIAO {2,3,4,9,10,11,12,13,A0,A1,A2,A3}
/*PWM引脚*/
enum PWM_YINJIAO {3,9,10,11}
/*模拟引脚*/
enum MN_YINJIAO {A0,A1,A2,A3}
/*数字传感器有无信号下拉*/
enum YOUWU {
    //% block="有信号"
    0,
    //% block="无信号"
    1
}
enum shou_shi{
    //% block="向上"
    DIR_UP,
    //% block="向下"
    DIR_DOWN,
    //% block="向左"
    DIR_LEFT,
    //% block="向右"
    DIR_RIGHT,
    //% block="逐渐靠近"
    DIR_NEAR,
    //% block="逐渐远离"
    DIR_FAR
}
/*Max7219点阵屏三个引脚下拉*/
enum din {2,3,4,9,10,11,12,13,A0,A1,A2,A3}
enum cs  {2,3,4,9,10,11,12,13,A0,A1,A2,A3}
enum clk {2,3,4,9,10,11,12,13,A0,A1,A2,A3}
/*Max7219点阵屏显示内容下拉（取模数据）*/
enum DZ_XianShi{
    0,1,2,3,4,5,6,7,8,9,
    //% block="A"
    10,
    //% block="B"
    11,
    //% block="C"
    12,
    //% block="D"
    13,
    //% block="E"
    14,
    //% block="F"
    15,
    //% block="G"
    16,
    //% block="H"
    17,
    //% block="I"
    18,
    //% block="J"
    19,
    //% block="K"
    20,
    //% block="L"
    21,
    //% block="M"
    22,
    //% block="N"
    23,
    //% block="O"
    24,
    //% block="P"
    25,
    //% block="Q"
    26,
    //% block="R"
    27,
    //% block="S"
    28,
    //% block="T"
    29,
    //% block="U"
    30,
    //% block="V"
    31,
    //% block="W"
    32,
    //% block="X"
    33,
    //% block="Y"
    34,
    //% block="Z"
    35,
    //% block="中"
    36,
    //% block="国"
    37,
    //% block="你"
    38,
    //% block="斜杠"
    39,
    //% block="测试"
    40,
}
//动画
enum DZ_DONG_HUA
{
    //% block="依次点亮"
    1,
    //% block="缩放"
    2,
    //% block="倒计时"
    3,
}
/*-----语音识别-----*/
enum MOSHI {
    //% block="循环识别模式，任何语句直接识别"
    LOOP_MODE,
    //% block="口令模式，以第一个词条为口令"
    BUTTON_MODE,
    //% block="按键模式，按下开始识别"
    KEYWORDS_MODE,
    //% block="口令或按键模式"
    KEYWORDS_AND_BUTTON
}

/*-----陀螺仪-----*/
enum ZuoBiaoZhou {
    //% block="X轴"
    BT_X,
    //% block="Y轴"
    BT_Y,
    //% block="Z轴"
    BT_Z
}
/*--------DS1302时钟模块--------*/
enum DS1302_RST {2,3,4,9,10,11,12,13,A0,A1,A2,A3}
enum DS1302_DAT {2,3,4,9,10,11,12,13,A0,A1,A2,A3}
enum DS1302_CLK {2,3,4,9,10,11,12,13,A0,A1,A2,A3}

enum RTC_T {
    //% block="年"
    Year,
    //% block="月"
    Month,
    //% block="日"
    Day,
    //% block="时"
    Hour,
    //% block="分"
    Minute,
    //% block="秒"
    Second,
    //% block="周"
    DayOfWeek
}
//% color="#696969" iconWidth=40 iconHeight=40  /*#FF7000*/
namespace LBXQ3 
{
    //--------------Max7219点阵屏模块-------------------//

    //% block="初始化 点阵屏 DIN[din]CS[cs]CLK[clk]" blockType="command"
    //% din.shadow="dropdownRound" din.options="din" din.defl="din.2"
    //% cs.shadow="dropdownRound" cs.options="cs" cs.defl="cs.3"
    //% clk.shadow="dropdownRound" clk.options="clk" clk.defl="clk.4"
    export function Max7219_DianZhen_CSH(parameter: any, block: any) 
    {
        let din = parameter.din.code;
        let cs = parameter.cs.code;
        let clk = parameter.clk.code;
        Generator.addInclude('LBXQ_3', '#include <LBXQ_3.h>');
        Generator.addSetup(`YING_JIAO_HUO_QU`,`YING_JIAO_HUO_QU(${din},${cs},${clk});`);
        //Generator.addSetup(`max7219.begin`,`max7219.begin(${din},${clk},${cs});`); //mind+自带库引用
        Generator.addSetup(`Init_MAX7219`,`Init_MAX7219();`);  
    }
    //点阵显示预设内容

    //% block="点阵屏 显示[DZ_XianShi]" blockType="command"
    //% DZ_XianShi.shadow="dropdownRound" DZ_XianShi.options="DZ_XianShi" DZ_XianShi.defl="DZ_XianShi.0"
    export function Max7219_DianZhen_XS1(parameter: any, block: any) 
    {
        let DZ_XianShi = parameter.DZ_XianShi.code;
        Generator.addCode(`Xian_Shi(${DZ_XianShi});`);
    }
    //点阵显示预设动画

    //% block="点阵屏 动画[DZ_DONG_HUA]间隔时间[DZ_T]" blockType="command"
    //% DZ_DONG_HUA.shadow="dropdownRound" DZ_DONG_HUA.options="DZ_DONG_HUA" DZ_DONG_HUA.defl="DZ_DONG_HUA.3"
    //% DZ_T.shadow="range" DZ_T.params.min="1" DZ_T.params.max="1000" DZ_T.defl="100"
    export function Max7219_DianZhen_DH(parameter: any, block: any) 
    {
        let DZ_DONG_HUA = parameter.DZ_DONG_HUA.code;
        let DZ_T = parameter.DZ_T.code;
        Generator.addCode(`Dong_hua(${DZ_DONG_HUA},${DZ_T});`);
    }

    //--------------------------------------------------------------------------//
    //------------------------------语音识别模块---------------------------------//
    //--------------------------------------------------------------------------//

    //% block="初始化 语音识别 模式[MOSHI]" blockType="command"
    //% MOSHI.shadow="dropdownRound" MOSHI.options="MOSHI" MOSHI.defl="MOSHI.1"
    export function YU_YIN_SHI_BIE(parameter: any, block: any) 
    {
        let MOSHI = parameter.MOSHI.code;

        //Generator.addInclude('asr_ld3320', '#include <asr_ld3320.h>');
        Generator.addInclude('LBXQ_3', '#include <LBXQ_3.h>');
        Generator.addSetup(`E_WORK_MODE asr_mode`,`E_WORK_MODE asr_mode = ${MOSHI};`);  
        Generator.addSetup(`Wire.begin`,`Wire.begin();`); 
        //Generator.addSetup(`Serial.begin`,`Serial.begin(9600);`); 
        Generator.addSetup(`ld3320_reset`,`ld3320_reset();`); // 复位语音识别模块
        Generator.addSetup(`ld3320_config_mode`,`ld3320_config_mode(asr_mode);`);// 识别模式设置
    }

    //% block="添加识别词条 编号[BIANHAO]内容[NEIRONG]使用拼音，字间用空格隔开" blockType="command"
    //% BIANHAO.shadow="range"   BIANHAO.params.min=1    BIANHAO.params.max=255    BIANHAO.defl=1
    //% NEIRONG.shadow="string"  NEIRONG.defl="ni hao"
    export function TIAN_JIA_CI_TIAO(parameter: any, block: any) 
    {
        let BIANHAO = parameter.BIANHAO.code;
        let NEIRONG = parameter.NEIRONG.code;
        Generator.addSetup(`ld3320_add_words_${BIANHAO}`,`ld3320_add_words(${BIANHAO},${NEIRONG});`);
    }

    //% block="词条添加完成 开始识别" blockType="command"
    export function KAI_SHI_SHI_BIE(parameter: any, block: any) 
    {
        Generator.addSetup(`ld3320_asr_start`,`ld3320_asr_start();`);
    }

    //% block="loop刷新一次识别结果" blockType="command"
    export function HUO_QU_JIE_GUO(parameter: any, block: any) 
    {
        Generator.addCode(`unsigned char result;`);
        Generator.addCode(`result = ld3320_get_result();`);
        Generator.addCode(`delay(20);`);//正常应该放在识别后每个操作后面，避免多次输出
    }

    //% block="识别结果" blockType="reporter"
    export function JIE_GUO(parameter: any, block: any) 
    {
        Generator.addCode(`result`);
    }

    //--------------手势识别模块-------------------//
    //% block="初始化 手势识别传感器 SCL SDA" blockType="command"
    export function Shou_shi_int(parameter: any, block: any) 
    {
        Generator.addInclude('Wire', '#include <Wire.h>');
        Generator.addInclude('LBXQ_3', '#include <LBXQ_3.h>');
        Generator.addObject( `SparkFun_APDS9960`, `SparkFun_APDS9960`,`apds = SparkFun_APDS9960();`);
        //Generator.addSetup(`attachInterrupt`,`attachInterrupt(0, isr_flag = 1, FALLING);`);//初始化中断
        Generator.addSetup(`apds.init`,`apds.init();`);  // 初始化APDS-9960(配置I2C和初始值)
        Generator.addSetup(`apds.enableGestureSensor`,`apds.enableGestureSensor(true);`);  // 开始运行APDS-9960手势传感器引擎
    }
    //% block="手势传感器 有数据可读？" blockType="boolean"
    export function Shou_shi_shujukedu(parameter: any, block: any) 
    {
        Generator.addCode(`apds.isGestureAvailable()`);
    }
    //% block="读取手势数据" blockType="reporter"
    export function Shou_shi_dushuju(parameter: any, block: any) 
    {
        
        Generator.addCode(`apds.readGesture()`);
    }
    //% block=" = [shou_shi]手势" blockType="reporter"
    //% shou_shi.shadow="dropdownRound" shou_shi.options="shou_shi" shou_shi.defl="shou_shi.DIR_UP"
    export function Shou_shi_dushoushi(parameter: any, block: any) 
    {
        let shou_shi = parameter.shou_shi.code;
        
        Generator.addCode(`${shou_shi}`);
    }

    //--------------陀螺仪模块-------------------//
    //% block="初始化 陀螺仪传感器 SCL SDA" blockType="command"
    export function Tuo_luo_yi_CSH(parameter: any, block: any) 
    {
        //Generator.addInclude('Mpu6050Module', '#include <Mpu6050Module.h>');
        Generator.addInclude('LBXQ_3', '#include <LBXQ_3.h>');
        Generator.addObject( `Mpu6050Module`, `Mpu6050Module`,`mpu6050;`);
        Generator.addSetup(`mpu6050.InitMpu6050`,`mpu6050.InitMpu6050();`);  // 初始化APDS-9960(配置I2C和初始值)
    }
    //% block="陀螺仪 刷新数据" blockType="command"
    export function Tuo_luo_yi_shauxin(parameter: any, block: any) 
    {
        Generator.addCode(`mpu6050.GetInclination();`);
    }
    //% block="读取陀螺仪[ZuoBiaoZhou]数据" blockType="reporter"
    //% ZuoBiaoZhou.shadow="dropdownRound" ZuoBiaoZhou.options="ZuoBiaoZhou" ZuoBiaoZhou.defl="ZuoBiaoZhou.BT_X"
    export function Tuo_luo_yi_dushuju(parameter: any, block: any) 
    {
        let ZuoBiaoZhou = parameter.ZuoBiaoZhou.code;
        
        Generator.addCode(`mpu6050.GetGravity(${ZuoBiaoZhou})`);
    }

    //--------------串口蓝牙测试-读数据------------------//
    
    ///% block="蓝牙 读取一次数据" blockType="command"
    export function lanya_du_qu(parameter: any, block: any) 
    {
        Generator.addInclude('LBXQ_3', '#include <LBXQ_3.h>');//统一分类到LBXQ
        Generator.addObject( `ly`, `lan_ya`,`ly;`);
        Generator.addSetup(`Serial.begin`,`Serial.begin(9600);`);
        Generator.addCode(`ly.du_qu();`);
    }

    //--------------串口蓝牙测试-数据------------------//
    
    ///% block="蓝牙 读取到的数据" blockType="reporter"

    export function lanya_shu_ju(parameter: any, block: any) 
    {
        Generator.addCode(`ly.shu_ju()`);
    } 

    //--------------DS1302初始化------------------//
    
    //% block="初始化 时钟DS1302 CLK[DS1302_CLK]DAT[DS1302_DAT]RST[DS1302_RST]" blockType="command"
    //% DS1302_CLK.shadow="dropdownRound" DS1302_CLK.options="DS1302_CLK" DS1302_CLK.defl="DS1302_CLK.2"
    //% DS1302_DAT.shadow="dropdownRound" DS1302_DAT.options="DS1302_DAT" DS1302_DAT.defl="DS1302_DAT.3"
    //% DS1302_RST.shadow="dropdownRound" DS1302_RST.options="DS1302_RST" DS1302_RST.defl="DS1302_RST.4"
    export function DS1302_CSH(parameter: any, block: any) 
    {
        let DS1302_RST = parameter.DS1302_RST.code;
        let DS1302_DAT = parameter.DS1302_DAT.code;
        let DS1302_CLK = parameter.DS1302_CLK.code;

        Generator.addInclude('ThreeWire', '#include <ThreeWire.h>');
        Generator.addInclude('RtcDS1302', '#include <RtcDS1302.h>');
        Generator.addObject( `myWire`, `ThreeWire`,`myWire(${DS1302_DAT},${DS1302_CLK},${DS1302_RST});`);
        Generator.addObject( `RtcDS1302<ThreeWire>`, `RtcDS1302<ThreeWire>`,`Rtc(myWire);`);

        Generator.addSetup(`Rtc.Begin`,`Rtc.Begin();`);
        Generator.addSetup(`Rtc.SetIsRunning`,`Rtc.SetIsRunning(true);`);
    }

    //--------------读取-------------------//

    //% block="RTC获取时间[RTC_T]" blockType="boolean"
    //% RTC_T.shadow="dropdownRound" RTC_T.options="RTC_T" RTC_T.defl="1"
    
    export function DS1302_DQ(parameter: any, block: any) 
    {
        let RTC_T = parameter.RTC_T.code;

        Generator.addCode(`Rtc.GetDateTime().${RTC_T}()`);
    }
    //--------------设置时间-------------------//

    //% block="RTC校准时间为[RTC_N]年[RTC_Y]月[RTC_R]日[RTC_S]时[RTC_F]分[RTC_M]秒" blockType="command"

    //% RTC_N.shadow="number"  RTC_N.defl=0
    //% RTC_Y.shadow="range"   RTC_Y.params.min=1    RTC_Y.params.max=12    RTC_Y.defl=1
    //% RTC_R.shadow="range"   RTC_R.params.min=1    RTC_R.params.max=31    RTC_R.defl=1
    //% RTC_S.shadow="range"   RTC_S.params.min=0    RTC_S.params.max=23    RTC_S.defl=0
    //% RTC_F.shadow="range"   RTC_F.params.min=0    RTC_F.params.max=59    RTC_F.defl=0
    //% RTC_M.shadow="range"   RTC_M.params.min=0    RTC_M.params.max=59    RTC_M.defl=0

    export function DS1302_SZ(parameter: any, block: any) 
    {
        let RTC_N = parameter.RTC_N.code;
        let RTC_Y = parameter.RTC_Y.code;
        let RTC_R = parameter.RTC_R.code;
        let RTC_S = parameter.RTC_S.code;
        let RTC_F = parameter.RTC_F.code;
        let RTC_M = parameter.RTC_M.code;
        
        switch(RTC_Y)
        {
            case 1: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Jan/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 2: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Feb/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 3: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Mar/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 4: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Apr/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 5: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("May/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 6: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Jun/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 7: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Jul/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 8: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Aug/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 9: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Sep/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 10: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Oct/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 11: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Nov/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            case 12: Generator.addSetup(`Rtc.SetDateTime`,`Rtc.SetDateTime(RtcDateTime("Dec/${RTC_R}/${RTC_N}", "${RTC_S}:${RTC_F}:${RTC_M}"));`); break;
            default: break;
        }
}